package it.happyisland.framework.web.core;

import javax.servlet.ServletException;

public class Converter {
	public Object convert(String value, Class<?> type) throws ServletException{
		if(type.equals(String.class)){
			return value;
		}
		if(type.equals(int.class) || type.equals(Integer.class) ){
			int result;
			if (value == null || value == "") {
				result = 0;
			} else {
				result = new Integer(value);
			}
			return result;
		}
		if(type.equals(boolean.class) || type.equals(Boolean.class) ){
			return Boolean.valueOf(value);
		}
		if(type.equals(char.class) || type.equals(Character.class) ){
			if(value.length() == 0) return null; 
			return value.charAt(0);
		}
		throw new ServletException("Cannot convert parameter annotated with Param to " + type);
	}
}
